package com.accessibility.talkingmemories;

import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TagActivity extends Activity{
	
	private boolean isRecording = false;
	MediaRecorder recorder = new MediaRecorder();;
	private Button start, stop;
	File path;
	private static String TAG = "AUDIO_RECORDING";
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tagactivity);
        start = (Button)findViewById(R.id.start_button);
        start.setOnClickListener(startListener);
        stop = (Button)findViewById(R.id.stop_button);
        stop.setOnClickListener(stopListener);
        
        
        
        resetRecorder();
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
		recorder.release();
	}

	private void resetRecorder() {
		try{
	    
		
	    path = new File(Environment.getExternalStorageDirectory(),"myRecording.mp4");
	    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
		recorder.setOutputFile(path.getAbsolutePath());
		
		}catch(Exception e){
			Log.w(TAG, e.getMessage().toString());
		}
		try {
			recorder.prepare();
		} catch(Exception e) {
			Log.w(TAG, e.getMessage().toString());
			e.printStackTrace();
		}
		
	}

		
	private View.OnClickListener startListener = new View.OnClickListener() {
		public void onClick(View v) {
			try {
				Log.d(TAG, "Inside startListenter try block");
				recorder.start();
				
				start.setEnabled(false);
				stop.setEnabled(true);
			} catch (Exception e)  {
				e.printStackTrace();
			}
			
		}
	};
	
		
		private View.OnClickListener stopListener = new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG, "Inside stopListenter");
				recorder.stop();
				resetRecorder();
				start.setEnabled(true);
				stop.setEnabled(false);
			}
		};
	}
	
	



